package com.action.service.pressenter;

import com.action.childbase.ChildBasePressent;
import com.action.entity.WXPAYBEAN;
import com.action.service.ChildRestServer;
import com.base.app_core.base.ActionPacketView;
import com.base.app_core.model.Paginated;
import com.base.app_core.model.RedPacketVo;
import com.base.app_core.service.Retrofit.RestServer;
import com.base.app_core.service.rx.Callback;
import com.base.app_core.util.AppConstants;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.HashMap;
import java.util.Map;

import javax.inject.Inject;

/**
 * Created by Administrator on 19/03/2018.
 */

public class GiveRedPacketPresenter extends ChildBasePressent<ActionPacketView.GiveRedPacketView> {

    @Inject
    public GiveRedPacketPresenter(ChildRestServer mRestfull, RestServer server) {
        super(mRestfull, server);
    }

    /**
     * 发红包
     *
     * @param notice
     * @param num
     * @param account
     * @param password
     * @param range
     * @param lng
     * @param lat
     * @param provinceCode
     * @param cityCode
     * @param areaCode
     */
    public void sendPacket(String notice, int num, BigDecimal account, String password, int range,
                           String lng, String lat, String img, String provinceCode, String cityCode, String areaCode, String urlcontent) {
        Map<String, Object> map = new HashMap<>();
        map.put(AppConstants.FIELD.CONTENT, notice);
        map.put(AppConstants.FIELD.NUM, num);
        map.put(AppConstants.FIELD.ACCOUNT, account.setScale(2, RoundingMode.UP).toPlainString());
        map.put(AppConstants.FIELD.PASSWORD, password);
        map.put(AppConstants.FIELD.RANGE, range);
        map.put(AppConstants.FIELD.LNG, lng);
        map.put(AppConstants.FIELD.LAT, lat);
        map.put(AppConstants.FIELD.IMGS, img);
        map.put(AppConstants.FIELD.PROVINCE_CODE, provinceCode);
        map.put(AppConstants.FIELD.CITY_CODE, cityCode);
        map.put(AppConstants.FIELD.AREA_CODE, areaCode);
        map.put(AppConstants.FIELD.URL, urlcontent);
        invoke(mChildRest.sendPacket(map),
                new Callback<RedPacketVo>() {
                    @Override
                    public void onResponse(RedPacketVo data, Paginated paginated,Object extraData) {
                        if (isnotNull()){
                            getView().onGiveSuccess(data);
                        }
                    }
                });
    }

    //支付
    public void WXPay(String data) {
        Map<String, Object> map = new HashMap<>();
        map.put(AppConstants.FIELD.ORDERCODE, data);
        invoke(mChildRest.wXPay(map), new Callback<WXPAYBEAN>());
    }

    //支付宝支付
    public void AliPay(String data) {
        Map<String, Object> map = new HashMap<>();
        map.put(AppConstants.FIELD.ORDERCODE, data);
        invoke(mChildRest.AliPay(map), new Callback<String>() {
            @Override
            public void onResponse(String data, Paginated paginated,Object extraData) {
                if (isnotNull()) {
                    super.onResponse(data, paginated,extraData);
                    if (mLifecycle.get() instanceof ActionPacketView.payView)
                        ((ActionPacketView.payView) mLifecycle.get()).AliPayRequest(data);
                }
            }
        });
    }

}
